package com.qyhl.cloud.webtv.module_integral.message;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.snackbar.Snackbar;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_integral.R;
import com.qyhl.cloud.webtv.module_integral.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.intergral.MessageBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

@Route(path = ARouterPathConstant.MESSAGE)
public class MessageActivity extends BaseActivity implements MessageContract.MessageView {
    @BindView(R2.id.message_title)
    TextView messageTitle;
    @BindView(R2.id.message_recyclerview)
    RecyclerView messageRecyclerview;
    @BindView(R2.id.root_layout)
    LinearLayout rootLayout;
    @BindView(R2.id.message_refresh)
    SmartRefreshLayout messageRefresh;

    private List<MessageBean> mDataList = new ArrayList<>();
    private EmptyWrapper<MessageBean> emptyWrapper;

    private String lastDate = "";

    private MessagePresenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.inter_activity_message;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new MessagePresenter(this);
        inits();

    }

    @Override
    protected void setListener() {
        setListeners();
        String userName = CommonUtils.getInstance().getUserName();
        mPresenter.getMessage(userName, lastDate);
    }

    private void inits() {
        messageTitle.setText("消息中心");
        messageRefresh.setRefreshFooter(new ClassicsFooter(this));
        messageRefresh.setRefreshHeader(new MaterialHeader(this));
        messageRefresh.setEnableLoadMore(true);

        messageRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        CommonAdapter<MessageBean> commonAdapter = new CommonAdapter<MessageBean>(this, R.layout.inter_item_message, mDataList) {
            @Override
            protected void convert(ViewHolder viewHolder, MessageBean messageBean, int i) {
                ImageView tag = viewHolder.getView(R.id.item_message_tag);
                TextView title = viewHolder.getView(R.id.item_message_title);
                TextView date = viewHolder.getView(R.id.item_message_date);
                TextView detail = viewHolder.getView(R.id.item_message_detail);
                TextView contentMain = viewHolder.getView(R.id.item_message_content_main);
                TextView contentDesciption = viewHolder.getView(R.id.item_message_content_description);

                if (messageBean.getType() == 1) {
                    title.setText("系统通知");
                    contentMain.setText("汇率： 1：" + messageBean.getRate());
                } else {
                    title.setText("提现通知");
                    contentMain.setText("金额：" + messageBean.getMoney() + "元");
                }

                date.setText(messageBean.getDate());

                if (messageBean.getFlag() == 0) {//未读
                    tag.setVisibility(View.VISIBLE);

                    title.setTextColor(getResources().getColor(R.color.global_black_lv1));

                    date.setTextColor(getResources().getColor(R.color.global_black_lv2));

                    detail.setTextColor(getResources().getColor(R.color.global_black_lv1));

                    contentMain.setTextColor(getResources().getColor(R.color.global_base));
                    SpanUtils spanUtils = new SpanUtils(MessageActivity.this);
                    switch (messageBean.getType()) {
                        case 1:
                            spanUtils.append("您于" + DateUtils.msgDateTranslate(messageBean.getDate()) + "申请兑换的").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal))
                                    .append(messageBean.getCoin() + "金币").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_special))
                                    .append("，已帮您兑换为零钱：").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal))
                                    .append(messageBean.getMoney() + "元").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_special));
                            break;
                        case 2:
                            spanUtils.append("您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal))
                                    .append(messageBean.getMoney() + "元零钱").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_special))
                                    .append("已提现至您" + (messageBean.getPlatform() == 1 ? "支付宝" : "微信") + "账户")
                                    .setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal));
                            break;
                        case 3:
                            spanUtils.append("您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal))
                                    .append(messageBean.getMoney() + "元零钱").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_special))
                                    .append("，因账户原因审核不通过")
                                    .setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal));
                            break;
                        case 4:
                            spanUtils.append("您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal))
                                    .append(messageBean.getMoney() + "元零钱").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_special))
                                    .append("，提现失败，零钱已原路返回")
                                    .setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal));
                            break;
                        default:
                            spanUtils.append("您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal))
                                    .append(messageBean.getMoney() + "元").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_special))
                                    .append("提现失败，金额已返回至零钱").setSpans(new TextAppearanceSpan(MessageActivity.this, R.style.inter_cash_tip_normal));
                            break;
                    }
                    contentDesciption.setText(spanUtils.create());
                } else {
                    tag.setVisibility(View.GONE);

                    title.setTextColor(getResources().getColor(R.color.global_gray_lv2));

                    date.setTextColor(getResources().getColor(R.color.global_gray_lv2));

                    detail.setTextColor(getResources().getColor(R.color.global_gray_lv2));

                    contentMain.setTextColor(getResources().getColor(R.color.global_gray_lv2));

                    contentDesciption.setTextColor(getResources().getColor(R.color.global_gray_lv2));
                    String msg;
                    switch (messageBean.getType()) {
                        case 1://金币兑换
                            msg = "您于" + DateUtils.msgDateTranslate(messageBean.getDate()) + "申请兑换的" + messageBean.getCoin() + "金币，已帮您兑换为零钱：" + messageBean.getMoney() + "元";
                            break;
                        case 2://提现成功
                            msg = "您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的" + messageBean.getMoney() + "元零钱，已提现至您"
                                    + (messageBean.getPlatform() == 1 ? "支付宝" : "微信") + "账户";
                            break;
                        case 3:
                            msg = "您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的" + messageBean.getMoney() + "元零钱，因账户原因审核不通过";
                            break;
                        case 4:
                            msg = "您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的" + messageBean.getMoney() + "元零钱，提现失败，零钱已原路返回";
                            break;
                        default://提现失败
                            msg = "您于" + DateUtils.msgDateTranslate(messageBean.getCashDate()) + "申请提现的" + messageBean.getMoney() + "元零钱，提现失败，金额已返回至零钱";
                            break;
                    }
                    contentDesciption.setText(msg);
                }

                detail.setOnClickListener(view -> RouterManager.router(ARouterPathConstant.WALLER));
            }
        };

        emptyWrapper = new EmptyWrapper<>(commonAdapter);
        emptyWrapper.setEmptyView(R.layout.inter_layout_common_emptyview);
        messageRecyclerview.setAdapter(emptyWrapper);


    }

    private void setListeners() {
        final String userName = CommonUtils.getInstance().getUserName();
        messageRefresh.setOnRefreshListener(refreshLayout -> {
            lastDate = "";
            mPresenter.getMessage(userName, lastDate);
        });

        messageRefresh.setOnLoadMoreListener(refreshLayout -> mPresenter.getMessage(userName, lastDate));
    }


    @OnClick({R2.id.back_btn, R2.id.message_clean})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.back_btn) {
            finish();

        } else if (i == R.id.message_clean) {
        }
    }

    @Override
    public void getMessageSuccess(List<MessageBean> beanList) {
        messageRefresh.finishRefresh();
        messageRefresh.finishLoadMore();
        if (StringUtils.isEmpty(lastDate)) {
            mDataList.clear();
            mDataList.addAll(beanList);
        } else {
            mDataList.addAll(beanList);
        }
        try {
            if (beanList.size() > 0) {
                lastDate = beanList.get(beanList.size() - 1).getDate();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        emptyWrapper.notifyDataSetChanged();
        String userName = CommonUtils.getInstance().getUserName();
        mPresenter.domark(userName);
    }

    @Override
    public void getMessageError(String msg) {
        Toasty.normal(this, msg).show();
        messageRefresh.finishRefresh();
        messageRefresh.finishLoadMore();
    }

    @Override
    public void netWorkError(String msg) {
        messageRefresh.finishRefresh();
        messageRefresh.finishLoadMore();
        Snackbar.make(rootLayout, msg, Snackbar.LENGTH_LONG).setAction("设置", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(
                                Settings.ACTION_WIFI_SETTINGS),
                        0);
            }
        }).show();
    }
}
